#!/nmsmw/python/bin/python

import os
import sys
import re
import time
import datetime
import cx_Oracle
from multiprocessing import Pool

remote_ora = {}#<<FOLD<<
#remote_ora['gk']        = 'boya/boya@10.128.75.65:1521/orcl.ntars.com'
remote_ora['gk']        = 'ibms/ibms@10.166.64.41:1521/orcl'
remote_ora['108']       = 'boya/boya@10.166.64.41:1521/orcl'
remote_ora['anhui']    = 'boya/boya@10.152.64.33:1521/orcl.idevelopment.info'
remote_ora['beijing']    = 'boya/boya@10.134.64.33:1521/orcl.ntars.com'
remote_ora['chongqing']    = 'boya/boya@10.137.64.33:1521/orcl'
remote_ora['fujian']    = 'boya/boya@10.162.64.33:1521/orcl.ntars.com'
remote_ora['gansu']    = 'boya/boya@10.142.64.33:1521/orcl'
remote_ora['guangdong']    = 'boya/boya@10.164.64.33:1521/orcl.ntars.com'
remote_ora['guangxi']    = 'boya/boya@10.161.64.33:1521/orcl'
remote_ora['guizhou']    = 'boya/boya@10.157.64.33:1521/orcl'
remote_ora['hainan']    = 'boya/boya@10.160.64.33:1521/orcl'
remote_ora['hebei']    = 'boya/boya@10.141.64.33:1521/orcl.ntars.com'
remote_ora['heilong']    = 'boya/boya@10.146.64.33:1521/orcl.ntars.com'
remote_ora['henan']    = 'boya/boya@10.151.64.33:1521/orcl.ntars.com'
remote_ora['hubei']     = 'boya/boya@10.155.64.33:1521/orcl.ntars.com'
remote_ora['hunan']    = 'boya/boya@10.158.64.33:1521/orcl.ntars.com'
remote_ora['jiangsu']    = 'boya/boya@10.150.64.33:1521/orclXDB.ntars.com'
remote_ora['jiangxi']    = 'boya/boya@10.159.64.33:1521/orcl.info'
remote_ora['jilin']    = 'boya/boya@10.147.64.33:1521/orcl'
remote_ora['liaoning']    = 'boya/boya@10.143.64.33:1521/orcl.ntars.info'
remote_ora['neimeng']    = 'boya/boya@10.135.64.33:1521/orcl'
remote_ora['ningxia']   = 'boya3w/boya3w@10.139.64.33:1521/orcl'
remote_ora['qinghai']    = 'boya/boya@10.144.64.33:1521/orcl.ntars.com'
remote_ora['shandong']  = 'boya/boya@10.154.64.33:1521/orcl.ntars.com'
#remote_ora['shandong']  = 'ntars/sddb_2008@10.154.64.33:1521/orcl.ntars.com'
remote_ora['shanghai']    = 'boya/boya@10.163.64.33:1521/orcl.ntars.com'
remote_ora['shanxi']    = 'boya/boya@10.140.64.33:1521/orcl.ntars.com'
remote_ora['sanxi']    = 'boya/boya@10.145.64.33:1521/orcl.ntars.com'
remote_ora['sichuan']    = 'boya/boya@10.138.64.33:1521/orcl.ntars.com'
remote_ora['tianjin']    = 'boya/boya@10.148.64.33:1521/orcl'
remote_ora['xinjiang']    = 'boya/boya@10.149.64.33:1521/orcl.ntars.com'
remote_ora['xizang']    = 'boya/boya@10.136.64.33:1521/orcl.ntars.com'
remote_ora['yunnan']    = 'boya/boya@10.156.64.33:1521/orcl'
remote_ora['zhejiang']    = 'boya/boya@10.153.64.33:1521/orcl.ntars.com'
remote_ora['ipv6']    = 'boya/boya@10.128.75.6:1521/orcl'
#>>FOLD>>

remote_rac = {}#<<FOLD<<
#remote_rac['gk']        = 'boya/boya@10.128.75.67:1521/orcl.ntars.com'
remote_rac['anhui']    = 'boya/boya@10.152.64.34:1521/orcl.idevelopment.info'
remote_rac['beijing']    = 'boya/boya@10.134.64.34:1521/orcl.ntars.com'
remote_rac['chongqing']    = 'boya/boya@10.137.64.34:1521/orcl'
remote_rac['fujian']    = 'boya/boya@10.162.64.34:1521/orcl.ntars.com'
remote_rac['gansu']    = 'boya/boya@10.142.64.34:1521/orcl'
remote_rac['guangdong']    = 'boya/boya@10.164.64.34:1521/orcl.ntars.com'
remote_rac['guangxi']    = 'boya/boya@10.161.64.34:1521/orcl'
remote_rac['guizhou']    = 'boya/boya@10.157.64.34:1521/orcl'
remote_rac['hainan']    = 'boya/boya@10.160.64.34:1521/orcl'
remote_rac['hebei']    = 'boya/boya@10.141.64.34:1521/orcl.ntars.com'
remote_rac['heilong']    = 'boya/boya@10.146.64.34:1521/orcl.ntars.com'
remote_rac['henan']    = 'boya/boya@10.151.64.34:1521/orcl.ntars.com'
remote_rac['hubei']     = 'boya/boya@10.155.64.34:1521/orcl.ntars.com'
remote_rac['hunan']    = 'boya/boya@10.158.64.34:1521/orcl.ntars.com'
remote_rac['jiangsu']    = 'boya/boya@10.150.64.34:1521/orclXDB.ntars.com'
remote_rac['jiangxi']    = 'boya/boya@10.159.64.34:1521/orcl.info'
remote_rac['jilin']    = 'boya/boya@10.147.64.34:1521/orcl'
remote_rac['liaoning']    = 'boya/boya@10.143.64.34:1521/orcl.ntars.info'
remote_rac['neimeng']    = 'boya/boya@10.135.64.34:1521/orcl'
remote_rac['ningxia']   = 'boya3w/boya3w@10.139.64.34:1521/orcl'
remote_rac['qinghai']    = 'boya/boya@10.144.64.34:1521/orcl.ntars.com'
remote_rac['shandong']  = 'boya/boya@10.154.64.34:1521/orcl.ntars.com'
remote_rac['shanghai']    = 'boya/boya@10.163.64.34:1521/orcl.ntars.com'
remote_rac['shanxi']    = 'boya/boya@10.140.64.34:1521/orcl.ntars.com'
remote_rac['sanxi']    = 'boya/boya@10.145.64.34:1521/orcl.ntars.com'
remote_rac['sichuan']    = 'boya/boya@10.138.64.34:1521/orcl.ntars.com'
remote_rac['tianjin']    = 'boya/boya@10.148.64.34:1521/orcl'
remote_rac['xinjiang']    = 'boya/boya@10.149.64.34:1521/orcl.ntars.com'
remote_rac['xizang']    = 'boya/boya@10.136.64.34:1521/orcl.ntars.com'
remote_rac['yunnan']    = 'boya/boya@10.156.64.34:1521/orcl'
remote_rac['zhejiang']    = 'boya/boya@10.153.64.34:1521/orcl.ntars.com'
#>>FOLD>>

mtx_tables = {}#<<FOLD<<
mtx_tables['MONITOR_FAULT']             = 'COLLECTOR_STATE_SPACE'
mtx_tables['MT_COLLECTOR_STATE']        = 'COLLECTOR_STATE_SPACE'
mtx_tables['MT_COLLECTOR_DIR']          = 'COLLECTOR_STATE_SPACE'
mtx_tables['MTX_PERF_NET']              = 'MTX_PERF_NET'
mtx_tables['MTXRPT_EVENT_M']            = 'EVENT_COUNT_SPACE'
mtx_tables['MTXRPT_DEVICE_M']           = 'EVENT_COUNT_SPACE'
mtx_tables['MTXRPT_EVENTMOBILE_M']      = 'EVENT_COUNT_SPACE'
mtx_tables['RPT_MTXMOBILE_DEVICE_M']    = 'EVENT_COUNT_SPACE'
mtx_tables['RPT_MTXMOBILE_EVENT_M']     = 'EVENT_COUNT_SPACE'
mtx_tables['RPT_MTXMOBILE_IMEI_M']      = 'EVENT_COUNT_SPACE'
mtx_tables['RPT_MTXMOBILE_INOUT_H']     = 'EVENT_COUNT_SPACE'
mtx_tables['RPT_MTXMOBILE_PROVINCE_H']  = 'EVENT_COUNT_SPACE'
mtx_tables['RPT_MTXMOBILE_PROVINCE_M']  = 'EVENT_COUNT_SPACE'
mtx_tables['MTX_EVENT_REALTIME']        = 'EVENT_COUNT_SPACE'
#>>FOLD>>

dns_tables = {}#<<FOLD<<
dns_tables['DOMAIN_AA']                     = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_CLIENT_COUNT']           = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_COUNT']                  = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_DNAME_LENGTH']           = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_FLUX']                   = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_OPCODE_COUNT']           = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_QUERY_PACKET_LENGTH']    = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_IMSI_TOPN']              = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_IMSI_TOP100']            = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_RA']                     = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_RCODE']                  = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_RD']                     = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_RESPONSE_PACKET_LENGTH'] = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_TC']                     = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_TTL_LENGTH']             = 'DOMAIN_DATA_SPACE'
dns_tables['DOMAIN_TYPE_COUNT']             = 'DOMAIN_DATA_SPACE'
dns_tables['ERROR_PKT']                     = 'DOMAIN_DATA_SPACE'
dns_tables['TOPN_CLIENT_BYTE']              = 'DOMAIN_DATA_SPACE'
dns_tables['TOPN_CLIENT_PACKET']            = 'DOMAIN_DATA_SPACE'
dns_tables['TOPN_DOMAIN_NAME']              = 'DOMAIN_DATA_SPACE'
dns_tables['TOPN_NXDOMAIN']                 = 'DOMAIN_DATA_SPACE'
dns_tables['TOPN_TLD']                      = 'DOMAIN_DATA_SPACE'
dns_tables['DNSHJK']                        = 'DOMAIN_DATA_SPACE'
dns_tables['ABNORMAL_BASE_DNS']             = 'DOMAIN_DATA_SPACE'
dns_tables['ABNORMAL_SPECIAL_QUEST']        = 'DOMAIN_DATA_SPACE'
dns_tables['ABNORMAL_SERVER_QA']            = 'DOMAIN_DATA_SPACE'
#>>FOLD>>

dnspod_tables = {}#<<FOLD<<
dnspod_tables['DNSPOD_IP_COUNT']             = 'DOMAIN_DATA_SPACE'
dnspod_tables['DNSPOD_IP_FLUX']              = 'DOMAIN_DATA_SPACE'
dnspod_tables['DNSPOD_LOCATION_COUNT']       = 'DOMAIN_DATA_SPACE'
dnspod_tables['DNSPOD_LOCATION_DOMAIN']      = 'DOMAIN_DATA_SPACE'
dnspod_tables['DNSPOD_PCI_COUNT']            = 'DOMAIN_DATA_SPACE'
dnspod_tables['DNSPOD_TOP100_DOMAIN']        = 'DOMAIN_DATA_SPACE'
dnspod_tables['DNSPOD_TOP_DOMAIN']           = 'DOMAIN_DATA_SPACE'
#>>FOLD>>

vds_tables = {}#<<FOLD<<
vds_tables['VDSRPT_DEVICE_M'] = 'EVENT_COUNT_SPACE'
vds_tables['VDSRPT_EVENT_M']  = 'EVENT_COUNT_SPACE'
#>>FOLD>>

ntars_tables = {}#<<FOLD<<
#ntars_tables['ROUTER_STAT_H'] = 'REPORT_H_SPACE'
#ntars_tables['ROUTER_IPSRCPACKET_H'] = 'REPORT_H_SPACE'
#ntars_tables['ROUTER_IPDSTBYTE_H'] = 'REPORT_H_SPACE'
#ntars_tables['ROUTER_IPDSTPACKET_H'] = 'REPORT_H_SPACE'
#ntars_tables['ROUTER_IPCOUNT_H'] = 'REPORT_H_SPACE'
#ntars_tables['ROUTER_CPORT_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_IPCOUNT_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_TOS_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_IPSRCBYTE_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_IPDSTBYTE_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_PEERIPDSTBYTE_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_PEERIPDSTPACKET_H'] = 'REPORT_H_SPACE'
#ntars_tables['FILTER_REPORT_H'] = 'REPORT_H_SPACE'
#ntars_tables['ROUTER_PROTOCOL_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_PROTOCOL_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_SRCPORT_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_IPDSTPACKET_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_DSTPORT_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_IPSRCPACKET_H'] = 'REPORT_H_SPACE'
#ntars_tables['ROUTER_REGION_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_PEERIPSRCPACKET_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_CPORT_H'] = 'REPORT_H_SPACE'
#ntars_tables['ROUTER_SRCPORT_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_PEERIPSRCBYTE_H'] = 'REPORT_H_SPACE'
#ntars_tables['ROUTER_TOS_H'] = 'REPORT_H_SPACE'
#ntars_tables['ROUTER_DSTPORT_H'] = 'REPORT_H_SPACE'
#ntars_tables['ROUTER_IPSRCBYTE_H'] = 'REPORT_H_SPACE'
#ntars_tables['CUSTOMER_REGION_H'] = 'REPORT_H_SPACE'
#ntars_tables['RG_CUSTOMER_SRCPORT_H'] = 'RG_CS_SRCPORT_H_TS'
ntars_tables['ROUTER_STAT_M'] = 'REPORT_M_SPACE'
ntars_tables['ROUTER_IPDSTBYTE_M'] = 'REPORT_M_SPACE'
ntars_tables['ROUTER_IPCOUNT_M'] = 'REPORT_M_SPACE'
ntars_tables['ROUTER_CPORT_M'] = 'REPORT_M_SPACE'
ntars_tables['ROUTER_PROTOCOL_M'] = 'REPORT_M_SPACE'
ntars_tables['ROUTER_REGION_M'] = 'REPORT_M_SPACE'
ntars_tables['ROUTER_SRCPORT_M'] = 'REPORT_M_SPACE'
ntars_tables['ROUTER_DSTPORT_M'] = 'REPORT_M_SPACE'
ntars_tables['ROUTER_IPSRCBYTE_M'] = 'REPORT_M_SPACE'
#>>FOLD>>

topn_tables = {}#<<FOLD<<
topn_tables['TOPN_CLIENT_BYTE']              = 'DOMAIN_DATA_SPACE'
topn_tables['TOPN_CLIENT_PACKET']            = 'DOMAIN_DATA_SPACE'
topn_tables['TOPN_DOMAIN_NAME']              = 'DOMAIN_DATA_SPACE'
topn_tables['TOPN_NXDOMAIN']                 = 'DOMAIN_DATA_SPACE'
topn_tables['TOPN_TLD']                      = 'DOMAIN_DATA_SPACE'
#>>FOLD>>

extra_tables = {}
extra_tables['ERROR_PKT'] = 'DOMAIN_DATA_SPACE'
extra_tables['MTXRPT_EVENT_M'] = 'DOMAIN_DATA_SPACE'
extra_tables['DOMAIN_IMSI_TOPN']              = 'DOMAIN_DATA_SPACE'
extra_tables['ABNORMAL_BASE_DNS'] = 'DOMAIN_DATA_SPACE'
extra_tables['ABNORMAL_SERVER_QA'] = 'DOMAIN_DATA_SPACE'
extra_tables['ABNORMAL_SPECIAL_QUEST'] = 'DOMAIN_DATA_SPACE'
extra_tables['DNSPOD_PCI_COUNT']            = 'DOMAIN_DATA_SPACE'
extra_tables['DNSPOD_TOP100_DOMAIN']        = 'DOMAIN_DATA_SPACE'
extra_tables['DNSPOD_TOP_DOMAIN']           = 'DOMAIN_DATA_SPACE'
extra_tables['DNSPOD_IP_COUNT']             = 'DOMAIN_DATA_SPACE'
extra_tables['DNSPOD_LOCATION_COUNT']       = 'DOMAIN_DATA_SPACE'
extra_tables['DNSHJK']             = 'DOMAIN_DATA_SPACE'

def StringToTime(strtime):#<<FOLD<<
    t_tuple = time.strptime(strtime,"%Y%m%d")
    return int(time.mktime(t_tuple))
#>>FOLD>>

def gen_sqls(operate, date):#<<FOLD<<
    #tables = dict(dict(dict(mtx_tables, **dns_tables), **dnspod_tables), **vds_tables)
    #tables = dict(dns_tables, **dnspod_tables)
    tables = mtx_tables
    sqls = []
    if 'add' == operate:
        date1 = StringToTime(date)
        for table in tables:
            sqls.append('alter table %s add partition PART_%s values less than(%s) tablespace %s'%(table, date, date1, tables[table]))
    elif 'drop' == operate:
        for table in tables:
            sqls.append('alter table %s drop partition PART_%s'%(table, date))
    else:
        print 'Wrong operation on table partitions, now quit.'
    return sqls
#>>FOLD>>

def ora_init(ora):#<<FOLD<<
    if ora in remote_ora.keys():
        #print ' ------>', ora, '<------'
        try:
            db = cx_Oracle.connect(remote_ora[ora])
            db.autocommit = True
            #print 'Connect to oracle server successfully ...'
        except:
            if ora in remote_rac.keys():
                #print 'Time out, now try to connect the rac ...'
                try:
                    db = cx_Oracle.connect(remote_rac[ora])
                    db.autocommit = True
                    #print 'Connect to oracle server successfully ...'
                except:
                    #print 'Error, Failed to connect oracle', ora
                    return
            else:
                #print 'Error, Failed to connect oracle', ora
                return
    else:
        #print 'Error, Wrong oracle', ora
        return
    return db
#>>FOLD>>

def operate_partitions(ora):#<<FOLD<<
    db = ora_init(ora)
    if not db:
        print '\n\033[31m %s Connect failed.\033[0m'%ora
        return (ora, 'Connect failed')
    cur = db.cursor()
    month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    for day in range(0, month[int(sys.argv[2])%100-1]):
        date = str(int(sys.argv[2])*100+1+day)
        sqls = gen_sqls(sys.argv[1], date)
        for sql in sqls:
            try:
                cur.execute(sql)
                sys.stdout.write('.')
                sys.stdout.flush()
            except Exception, e:
                matched = 0
                ignore_errors = ['ORA-00942', 'ORA-02149']
                for error in ignore_errors:
                    if error in str(e).upper():
                        matched = 1
                        sys.stdout.write('.')
                        sys.stdout.flush()
                        break
                if not matched:
                    print "\n\033[31mRUN \"%s\" ON %s failed:%s\033[0m"%(sql, ora, str(e))
    cur.close()
    db.close()
    print '\n\033[32m %s Complete.\033[0m'%ora
    return (ora, 'Complete')
#>>FOLD>>

def main():#<<FOLD<<
    if 0 == len(sys.argv[3:]):
        oras = remote_ora.keys()
    else:
        oras = sys.argv[3:]
    process_pool = Pool(8)
    result = process_pool.map_async(operate_partitions, oras)
    for res in result.get():
        print res
#>>FOLD>>

if __name__ == '__main__':
    if 2 < len(sys.argv) and sys.argv[2].isdigit():
        main()
    else:
        print 'Usage: %s {add|drop} 201603 [pc1, pc2 ...]'%sys.argv[0]

